﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls;//添加命名空间，可以使用OleDbConnection等对象
using System.Data.OleDb;//添加命名空间，可以使用dataset对象


public partial class adminstrator_editStudent : System.Web.UI.Page
{
    public int stuID;//用于存储从editStudent.aspx页面传递过来的数据
    public OleDbConnection myConnection;
    public static readonly string sConnectionString = System.Configuration.ConfigurationManager.AppSettings["SQLConnStriing"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sConnectionString);
        myConnection.Open();//打开数据库连接
        if (Session["UserName"] == null && Session["UserNo"] == null)//没有经过正常连接途径打开页面
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "alert('登录超时（或未登录），请您重新登陆！')；parent.location.herf='../login.asp'</script>", false);
        stuID = int.Parse(Request.QueryString["stuID"]);//获取editStudent.aspx页面传递过来的stuID
        if (!IsPostBack)//首次打开页面
            bind(stuID);//调用数据绑定函数
    }
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        string sSex;
        if (RadioButtonMan.Checked == true)
            sSex = "男";
        else
            sSex = "女";
        string mystrl = "update StudentTb set SName ='" + UserName.Text.Trim() + "',sSex='" + sSex + "',sNation'" +
            DropDownListNation.SelectedItem.Text + "',sCollege='" + DropDownListCollege.SelectedItem.Text.Trim() + "',sClass'" +
            UserClass.Text.Trim() + "',sPass='" + UserPass.Text.Trim() + "'where ID=" + stuID + "";
        OleDbCommand mycmd1 = new OleDbCommand(mystr1, myConnection);// OleDbCommand表示要对access数据源执行的sql语句或存储过程
        mycmd1.ExecuteNonQuery();
        string mystr2 = "update ScoreTb set SName ='" + UserName.Text.Trim() + "'where sNo='" + UserNo.Text.Trim() + "'";
        OleDbCommand mycmd2 = new OleDbCommand(mystr2, myConnection);
        mycmd2.ExecuteNonQuery();
        string mystr3 = "update SelcourseTb set SName ='" + UserName.Text.Trim() + "'where sNo='" + UserNo.Text.Trim() + "'";
        OleDbCommand mycmd3 = new OleDbCommand(mystr3, myConnection);
        mycmd3.ExecuteNonQuery();
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('学生信息修改成功！');</script>", false);
    }

    protected void ButtonReset_Click(object sender, EventArgs e)
    {
        bind(stuID);
    }
    public void bind(int id)
    {
        string mysql = "select * from StudentTb  where ID=" + id;
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataReader myReader = mycmd.ExecuteReader();//OleDbDataAdapter表示要对access数据源执行sql语句存储过程
        if (myReader.Read())
        { //***取出学生的各项信息
            UserNo.Text = myReader[1].ToString();
            UserName.Text = myReader[2].ToString();
            //***性别***
            if (myReader[2].ToString() == "男")
            {
                RadioButtonMan.Checked = true;

                RadioButtonWoman.Checked = false;
            }
            else
            {
                RadioButtonMan.Checked = false;
                RadioButtonWoman.Checked = true;
            }
            //***民族***
            if (myReader[4].ToString() == "汉族")//列表框的选项索引赋值0（选第1项）
                DropDownListNation.SelectedIndex = 0;
            else if (myReader[4].ToString() == "壮族")//列表框的选项索引赋值1（选第2项）
                DropDownListNation.SelectedIndex = 1;
            else if (myReader[4].ToString() == "瑶族")//列表框的选项索引赋值2（选第3项）
                DropDownListNation.SelectedIndex = 2;
            else if (myReader[4].ToString() == "苗族")//列表框的选项索引赋值3（选第4项）
                DropDownListNation.SelectedIndex = 3;
            else if (myReader[4].ToString() == "侗族")//列表框的选项索引赋值4（选第5项）
                DropDownListNation.SelectedIndex = 4;
            else if (myReader[4].ToString() == "壮族")//列表框的选项索引赋值5（选第6项）
                DropDownListNation.SelectedIndex = 1;
            else if (myReader[4].ToString() == "毛南族")//列表框的选项索引赋值6（选第7项）
                DropDownListNation.SelectedIndex = 5;
            else
                DropDownListNation.SelectedIndex = 6;
            //***学院***
            if (myReader[5].ToString() == "药学院")
                DropDownListNation.SelectedIndex = 0;
            else if (myReader[5].ToString() == "基础医学院")
                DropDownListNation.SelectedIndex = 1;
            else if (myReader[5].ToString() == "针灸推拿学院")
                DropDownListNation.SelectedIndex = 2;
            else if (myReader[5].ToString() == "壮医药学院")
                DropDownListNation.SelectedIndex = 3;
            else if (myReader[5].ToString() == "瑶医药学院")
                DropDownListNation.SelectedIndex = 4;
            else
                DropDownListNation.SelectedIndex = 5;
            UserClass.Text = myReader[6].ToString();
            UserPass.Text = myReader[7].ToString();
        }
        else //查询不到改学生用户
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('操作有误！');location。href=''</script>", false);


    }

}